﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using System.IO;
using DigiLib;

public partial class controls_admin_AdminModulesEdit : Admin_Controls_Base {
    protected void Page_Load(object sender, EventArgs e) {
        base.removeHGC(divMessage);
        if (!IsPostBack) {
            base.bindDDL(ddlParentMenu, BO_cms_admin_modules.Inside(0), "DESCRIPTION", "ADMIN_MODULE_ID");
            loadData();
        }
    }
    protected void loadData() {
        try {
            ObjDT = BO_cms_admin_modules.Search(Id);
            if (ObjDT != null && ObjDT.Rows.Count > 0) {
                txtMenuName.Text = ObjDT.Rows[0]["MODULE_NAME"].ToString();
                txtDesc.Text = ObjDT.Rows[0]["DESCRIPTION"].ToString();
                ddlParentMenu.SelectedValue = ObjDT.Rows[0]["PARENT_ID"].ToString();
                cboDisOnHome.Checked = Convert.ToBoolean(ObjDT.Rows[0]["IS_DISPLAY_ON_MENU"]);
                bool cp = Convert.ToBoolean(ObjDT.Rows[0]["IS_DISPLAY_ON_CP"]);
                cboCP.Checked = cp;
                txtShortDisplay.Text = ObjDT.Rows[0]["ORDER_DISPLAY"].ToString();
                if (!ObjDT.Rows[0]["THUMB_ICON"].ToString().Equals(String.Empty))
                    imgthumb.InnerHtml = "<img src='/" + Setting.PathAdmin + ObjDT.Rows[0]["THUMB_ICON"].ToString() + "' />";
                if (cp && !ObjDT.Rows[0]["CP_THUMB_ICON"].ToString().Equals(String.Empty))
                    imgCP.InnerHtml = "<img src='/" + Setting.PathAdmin + ObjDT.Rows[0]["CP_THUMB_ICON"].ToString() + "' />";
            }
        } catch (Exception ex) { divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message); }
    }
    protected void btnSubmit_Click(object sender, EventArgs e) {
        try {
            DataTable objDT = (DataTable)Session["admin"];
            ObjDT = BO_cms_admin_modules.Search(Id);
            string filename = ObjDT.Rows[0]["THUMB_ICON"].ToString();
            string thumbIcon = ObjDT.Rows[0]["CP_THUMB_ICON"].ToString();
            bool cp = cboCP.Checked;
            filename = base.Upload("cpIcon", filename, fulTHUMB_ICON, divMessage, true);
            if (cp) thumbIcon = base.Upload("cpIcon", thumbIcon, fulIcon, divMessage, true);
            int parent = Convert.ToInt32(ddlParentMenu.SelectedValue);
            if (parent == Id)
                parent = Convert.ToInt32(objDT.Rows[0]["PARENT_ID"]);
            string strResult = BO_cms_admin_modules.Update(Id, parent,
                txtMenuName.Text.Trim(), filename, thumbIcon, txtDesc.Text.Trim(),
                cboDisOnHome.Checked, cp,
                Convert.ToInt32(txtShortDisplay.Text.Trim()),
                 Convert.ToDateTime(ObjDT.Rows[0]["CREATED_DATE"].ToString()),
                ObjDT.Rows[0]["CREATED_BY"].ToString(), DateTime.Now,
                objDT.Rows[0]["USER_NAME"].ToString(),
                true);
            if (strResult != "0") {
                divMessage.InnerHtml = MessageBox.Success("Hệ Thống", "Đã cập nhật thông tin menu!");
                Response.Redirect("default.aspx?control=AdminModules&module_id=" + Module_ID.ToString() + "&keyword=" + Keyword);
            } else
                divMessage.InnerHtml = MessageBox.Error("Hệ Thống", "Có lỗi trong quá trình xử lý, thử lại sau!");
        } catch (Exception ex) { divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message); }
    }
}
